/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client.gui;

import java.util.Timer;
import javax.swing.JProgressBar;
import pl.cezary.butler.walili.client.TrafficLoggingTask;

/**
 *
 * @author czarek
 */
class TrafficUpdateProgressbarAction extends TrafficLoggingTask {
    private final JProgressBar trafficBar;

    public TrafficUpdateProgressbarAction(int coveredTime, final JProgressBar trafficBar) {
        super(coveredTime);
        this.trafficBar = trafficBar;
    }
    
    static Timer scheduleTrafficLogging(long frequency,JProgressBar progresBar){
        final Timer timer = new Timer(TrafficUpdateProgressbarAction.class.getSimpleName());
        timer.scheduleAtFixedRate(new TrafficUpdateProgressbarAction((int)(frequency*1.75),progresBar),frequency*2, frequency);
        progresBar.setString(formatProgress(0.));
        progresBar.setStringPainted(true);   
        return timer;
    }
    
    private static String formatProgress(final double kbytesPerSec){
        return String.format("%.2f kbytes/sec",kbytesPerSec);
    }
    


    @Override
    public void run() {
        super.run();
        final int trafficNow = (int) (kbytesPerSec * 100);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                int curMax = trafficBar.getMaximum();
                if (curMax < trafficNow) {
                    trafficBar.setMaximum(trafficNow);
                }
                trafficBar.setValue(trafficNow);
                trafficBar.setString(formatProgress(trafficNow / 100.0)); //TODO lokalizacja
                //TODO lokalizacja
            }
        });
    }
    
}
